<?php
require_once(ROOT . LIBRARY .'/fields/common.inc');

class fields_date extends fields_common implements field
{
   public $time      = FALSE;
   public $src_value = 0;

   public function  __construct($data)
   {
      parent::__construct($data);
      $this->escaped_columns[] = 'src_value';
      if(empty($this->src_value)) $this->src_value = strtotime($this->default);
   }

   /**
    * Normalize Value
    *
    * @param string $value
    * @param string $lang
    */
   public function normalizeValue($value, $lang = null)
   {
      $this->src_value = mysql2time($value);

      $lang = request::getLang('current');
      
      $value = '';
      if(!empty($this->src_value))
      {
         $dateformat = $lang['dateformat'];
         if(strpos($dateformat, 'm-local') !== FALSE)
         {
            $month = date('n', $this->src_value);
            $lang_key = strpos($dateformat, 'm-local-of') !== FALSE ? 'monthof' : 'month';
            $month = gettext::translate($lang_key, $month);
            if($lang_key == 'month') $dateformat = str_replace('m-local', '***', $dateformat);
            else $dateformat = str_replace('m-local-of', '***', $dateformat);
            $value = date($dateformat, $this->src_value);
            $value = str_replace('***', $month, $value);
            
         } else
         {
            $value = date($dateformat, $this->src_value);
         }
      }

      $this->value = $value;
      
      return $this->value;
   }


   /**
    * Get HTML code for Date fields
    *
    * @param object $data
    * @return string
    */
   public function getFieldHTML($data)
   {
      $value = $data->{$this->alias}('src');
      $value = ($this->time ? date('d.m.Y  H:i', $value) : date('d.m.Y', $value));

      $weekday_short = gettext::translate('weekday_short');
      $weekday = gettext::translate('weekday');
      $monthes = gettext::translate('month');
      ksort($monthes);

      $html = '<input type="text" name="f[' . $this->alias . ']" id="f_' . $this->alias . '" value="';
      $html .= $value . '" style="width:115px;" /> <span class="notice">99.99.9999'. ($this->time ? '  99:99' : '') .'</span>';
      $html .= '<script type="text/javascript">' . EOL;
      $html .= 'jQuery(document).ready(function(){' . EOL;
      $html .= '$("#f_' . $this->alias . '").mask("99.99.9999'. ($this->time ? ' 99:99' : '') .'");'. EOL;
      $html .= '});' . EOL;
      $html .= '</script>' . EOL;

      return $html;
   }


   private function getValue($data, $lang = NULL)
   {
      // TODO: реализовать возможность удаления даты, т.е. устанавливать в пустую
      $value = $data->{$this->alias}('src');
      return date('d.m.Y', $value);
   }


   public function validateInput($value, $objectData = NULL)
   {
      
      if (is_array($value) && isset($value['day'], $value['month'], $value['year']) &&
              checkdate($value['month'], $value['day'], $value['year']))
      {
         $value  = date('Y-m-d', mktime(1, 1, 1, $value['month'], $value['day'], $value['year'])) .' ';
         $value .= (!empty($value['hour']) && is_numeric($value['hour']) && $value['hour'] < 24 ? (strlen($value['hour']) == 1 ? '0'. $value['hour'] : $value['hour']) : '00') .':';
         $value .= (!empty($value['minute']) && is_numeric($value['minute']) && $value['minute'] < 60 ? (strlen($value['minute']) == 1 ? '0'.$value['minute'] : $value['hour']) : '00') .':00';
      } elseif (is_numeric($value) && $value > 0)
      {
         $value = date('Y-m-d H:i:00', $value);
      } else
      {
         preg_match_all('/\d+/', $value, $r);
         if (is_array($r) && count($r[0]) >= 3 && checkdate($r[0][1], $r[0][0], $r[0][2]))
         {
            $value  = $r[0][2] . '-' . $r[0][1] . '-' . $r[0][0] .' ';
            $value .= (!empty($r[0][3]) && is_numeric($r[0][3]) && $r[0][3] < 24 ? (strlen($r[0][3]) == 1 ? '0'. $r[0][3] : $r[0][3]) : '00') .':';
            $value .= (!empty($r[0][4]) && is_numeric($r[0][4]) && $r[0][4] < 60 ? (strlen($r[0][4]) == 1 ? '0'. $r[0][4] : $r[0][4]) : '00') .':00';

         } else
         {
            $value = date('Y-m-d H:i:00');
         }
      }

      $this->value = $value;
      
      return $this->value;
   }

}